package com.hupu.jrs.ui

import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.View
import com.hupu.jrs.util.SettingPrefUtil
import com.hupu.jrs.widget.swipeback.SwipeBackActivityBase
import com.hupu.jrs.widget.swipeback.SwipeBackActivityHelper
import com.hupu.jrs.widget.swipeback.SwipeBackLayout
import com.hupu.jrs.widget.swipeback.Utils

/**
 * Created by sll on 2015/9/10 0010.
 */
abstract class BaseSwipeBackActivity : BaseActivity(), SwipeBackActivityBase {

    private var mHelper: SwipeBackActivityHelper? = null

    override val swipeBackLayout: SwipeBackLayout
        get() = mHelper!!.swipeBackLayout!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mHelper = SwipeBackActivityHelper(this)
        mHelper!!.onActivityCreate()
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        mHelper!!.onPostCreate()
    }

    override fun <T : View?> findViewById(id: Int): T {
        val  v = super.findViewById<View>(id)
        return if (v == null && mHelper != null) mHelper!!.findViewById(id) as T else v as T
    }

    override fun setSwipeBackEnable(enable: Boolean) {
        swipeBackLayout.setEnableGesture(enable)
    }

    override fun scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this)
        swipeBackLayout.scrollToFinishActivity()
    }

    override fun finish() {
        super.finish()
    }

    fun initToolBar(mToolBar: Toolbar?) {
        if (null != mToolBar) {
            setSupportActionBar(mToolBar)
            supportActionBar!!.setDisplayShowHomeEnabled(false)
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        }
    }

    override fun onResume() {
        super.onResume()
        val mode = SettingPrefUtil.getSwipeBackEdgeMode(this)
        val mSwipeBackLayout = mHelper!!.swipeBackLayout!!
        when (mode) {
            0 -> mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT)
            1 -> mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_RIGHT)
            2 -> mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_BOTTOM)
            3 -> mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL)
        }
    }
}
